<?php

namespace App\Services;

use App\Models\User;
use App\Models\Post;
use App\Models\Comment;
use App\Models\Notification;

class NotificationService
{
    /**
     * 发送关注通知
     */
    public function sendFollowNotification(User $follower, User $following): void
    {
        $following->notify('follow', [
            'user_id' => $follower->id,
            'user_name' => $follower->name,
            'user_username' => $follower->username,
            'user_avatar' => $follower->avatar,
        ]);
    }

    /**
     * 发送评论通知
     */
    public function sendCommentNotification(Comment $comment): void
    {
        $post = $comment->post;
        $author = $post->user;

        // 不给自己发送通知
        if ($comment->user_id === $author->id) {
            return;
        }

        $author->notify('comment', [
            'user_id' => $comment->user_id,
            'user_name' => $comment->user->name ?? $comment->author_name,
            'post_id' => $post->id,
            'post_title' => $post->title,
            'post_slug' => $post->slug,
            'comment_id' => $comment->id,
            'comment_content' => mb_substr($comment->content, 0, 100),
        ]);
    }

    /**
     * 发送点赞通知
     */
    public function sendLikeNotification(User $user, Post $post): void
    {
        $author = $post->user;

        // 不给自己发送通知
        if ($user->id === $author->id) {
            return;
        }

        $author->notify('like', [
            'user_id' => $user->id,
            'user_name' => $user->name,
            'user_avatar' => $user->avatar,
            'post_id' => $post->id,
            'post_title' => $post->title,
            'post_slug' => $post->slug,
        ]);
    }

    /**
     * 发送文章审核通过通知
     */
    public function sendPostApprovedNotification(Post $post): void
    {
        $post->user->notify('post_approved', [
            'post_id' => $post->id,
            'post_title' => $post->title,
            'post_slug' => $post->slug,
            'approved_at' => now()->toDateTimeString(),
        ]);
    }

    /**
     * 发送文章审核拒绝通知
     */
    public function sendPostRejectedNotification(Post $post, string $reason): void
    {
        $post->user->notify('post_rejected', [
            'post_id' => $post->id,
            'post_title' => $post->title,
            'post_slug' => $post->slug,
            'reason' => $reason,
            'rejected_at' => now()->toDateTimeString(),
        ]);
    }

    /**
     * 发送@提及通知
     */
    public function sendMentionNotification(Comment $comment, User $mentionedUser): void
    {
        // 不给自己发送通知
        if ($comment->user_id === $mentionedUser->id) {
            return;
        }

        $mentionedUser->notify('mention', [
            'user_id' => $comment->user_id,
            'user_name' => $comment->user->name ?? $comment->author_name,
            'post_id' => $comment->post->id,
            'post_title' => $comment->post->title,
            'post_slug' => $comment->post->slug,
            'comment_id' => $comment->id,
            'comment_content' => mb_substr($comment->content, 0, 100),
        ]);
    }

    /**
     * 发送回复通知
     */
    public function sendReplyNotification(Comment $reply): void
    {
        $parentComment = $reply->parent;
        if (!$parentComment || !$parentComment->user_id) {
            return;
        }

        // 不给自己发送通知
        if ($reply->user_id === $parentComment->user_id) {
            return;
        }

        $parentComment->user->notify('reply', [
            'user_id' => $reply->user_id,
            'user_name' => $reply->user->name ?? $reply->author_name,
            'post_id' => $reply->post->id,
            'post_title' => $reply->post->title,
            'post_slug' => $reply->post->slug,
            'comment_id' => $reply->id,
            'parent_comment_id' => $parentComment->id,
            'reply_content' => mb_substr($reply->content, 0, 100),
        ]);
    }

    /**
     * 发送系统通知
     */
    public function sendSystemNotification(User $user, string $message, array $data = []): void
    {
        $user->notify('system', array_merge([
            'message' => $message,
        ], $data));
    }

    /**
     * 批量发送系统通知
     */
    public function broadcastSystemNotification(string $message, array $data = []): void
    {
        $users = User::where('status', 'active')->get();

        foreach ($users as $user) {
            $this->sendSystemNotification($user, $message, $data);
        }
    }

    /**
     * 解析评论中的@提及
     */
    public function parseAndNotifyMentions(Comment $comment): void
    {
        preg_match_all('/@([a-zA-Z0-9_]+)/', $comment->content, $matches);

        if (empty($matches[1])) {
            return;
        }

        $usernames = array_unique($matches[1]);
        $users = User::whereIn('username', $usernames)->get();

        foreach ($users as $user) {
            $this->sendMentionNotification($comment, $user);
        }
    }
}